package 语言特性学习;

import lombok.Data;
import lombok.ToString;
import org.junit.Test;

/**
 * Description
 *
 * @Author: SunAo
 * @Date: 2022/3/29 16:50
 */
public class 向上向下转型 {

    @Data
    @ToString
    public static class Parent{
        private String name;
        private String password;
    }

    @Data
    @ToString
    public static class Son extends Parent{
        private String skill;
    }

    @Test
    public void 父类转子类(){
        Parent p = new Parent();
        p.setName("hahaha");
        Son son = (Son) p;
        System.out.println(son);
    }

    @Test
    public void 子类转父类(){
        Son son = new Son();
        son.setName("Sun");
        Parent p = son;
        System.out.println(p.getName());
    }

    @Test
    public void 子类借父类转子类(){
        Son son = new Son();
        son.setName("Sunny");
        Parent p = son;
        Son son2 = (Son) p;
        System.out.println(p.getClass());
        System.out.println(son.getClass());
        System.out.println(son2.getClass());
        System.out.println(son2.getClass().equals(Son.class));
        System.out.println(son2.getName());
    }


    @Test
    public void 转为父类时判断(){
        Son son = new Son();
        son.setName("Sunny");
        Parent p = son;
        System.out.println(p instanceof Son);
    }


}
